//
//  RecBannerCell.swift
//

import UIKit
import FSPagerView

class RecBannerCell: UICollectionViewCell {
    
    var banners: [RecChildModel] = [] {
        didSet {
            bgImageView.isHidden = banners.isEmpty
            pagerView.reloadData()
            pageControl.numberOfPages = banners.count
        }
    }
    
    private lazy var bgImageView: UIImageView = {
        let view = UIImageView()
        view.image = "banner_bg_yellow".image
        view.isHidden = true
        return view
    }()
    
    private lazy var pagerView: FSPagerView = {
        let pagerView = FSPagerView()
        pagerView.dataSource = self
        pagerView.delegate = self
        pagerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "FSPagerViewCell")
        pagerView.isInfinite = true
        pagerView.scrollDirection = .horizontal
        pagerView.removesInfiniteLoopForSingleItem = true
        pagerView.automaticSlidingInterval = 5
        pagerView.backgroundColor = .clear
        pagerView.corner(radii: 8.fit)
        return pagerView
    }()

    private lazy var pageControl: FSPageControl = {
        let pageControl = FSPageControl()
        pageControl.hidesForSinglePage = true
        pageControl.contentHorizontalAlignment = .center
        pageControl.setFillColor(.white, for: .selected)
        pageControl.setFillColor(UIColor.c999999, for: .normal)
        pageControl.currentPage = 0
        pageControl.interitemSpacing = 12
        pageControl.backgroundColor = .clear
        return pageControl
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
           
        addSubviews()
    }
       
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addSubviews() {
        
        backgroundColor = .sysTableView
        addSubview(bgImageView)
        addSubview(pagerView)
        addSubview(pageControl)
        
        bgImageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(-1000)
            make.left.equalToSuperview().offset(-space)
            make.right.equalToSuperview().offset(space)
            make.height.equalTo(1130)
        }
        
        pagerView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        pageControl.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(20.fit)
        }
    }
}

extension RecBannerCell: FSPagerViewDataSource {
    public func numberOfItems(in pagerView: FSPagerView) -> Int {
        return banners.count
    }

    public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "FSPagerViewCell", at: index)
        cell.backgroundColor = .clear
        let model = banners[index]
        var imageUrl = model.data.bannercover
        if imageUrl.contains("@") {
            imageUrl = imageUrl.prefix("@")
        }
        var url = model.viewP.focusUrl
        if url.isEmpty {
            url = imageUrl
        }
        if url.isEmpty {
            url = model.data.image.prefix("@")
        }
        cell.imageView?.kf_set(url)
        return cell
    }
}

extension RecBannerCell: FSPagerViewDelegate {
    public func pagerViewDidScroll(_ pagerView: FSPagerView) {
        pageControl.currentPage = pagerView.currentIndex
    }

    public func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        pagerView.deselectItem(at: index, animated: false)
        
        guard let currVC = self.currVC else { return }
        let model = banners[index]
        debugPrint(model.layoutType)
        switch model.layoutType {
        case .vip:
            break
        case .video_player:
            let vc = PlayerViewController(url: model.data.url, id: model.data.subcateid)
            currVC.push(vc)
        case .audio_list:
            let vc = RecAudioListViewController()
            vc.id = model.id
            vc.atype = "1"
            vc.outerid = model.data.outerid
            currVC.push(vc)
        default:
            let vc = RecVideoMoreViewController()
            vc.navTitle = model.title
            vc.id = model.contentId
            vc.layoutType = model.layoutType
            currVC.push(vc)
        }
    }
}
